<?php

namespace App\Admin\Controllers;

use App\Models\Package;
use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\Shop;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PackagesController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.packages'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.packages'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.packages'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.packages'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Package);

        $grid->id('Id');
        $grid->shop(trans('admin.shop'))->display(function ($value) {
            return $value['name'];
        });
        $grid->title(trans('admin.title'));
        $grid->subtitle(trans('admin.subtitle'));
        $grid->image(trans('admin.image'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->content(trans('admin.content'))->limit(10);
        $grid->service_time(trans('admin.service_time'));
        $grid->price(trans('admin.price'));
        $grid->stock_amount(trans('admin.stock'));
        $grid->view_amount(trans('admin.view_amount'));
        $grid->sell_amount(trans('admin.sell_amount'));
        $grid->sort(trans('admin.sort'));
        $grid->created_at(trans('admin.created_at'));
        $grid->updated_at(trans('admin.updated_at'));
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {

            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('title', trans('admin.title'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->between('price', trans('admin.price'));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Package::findOrFail($id));

        $show->id('Id');
        $show->shop(trans('admin.shop'))->as(function ($value) {
            return $value['name'];
        });
        $show->title(trans('admin.title'));
        $show->subtitle(trans('admin.subtitle'));
        $show->image(trans('admin.image'))->image();
        $show->content(trans('admin.content'));
        $show->service_time(trans('admin.service_time'));
        $show->price(trans('admin.price'));
        $show->stock_amount(trans('admin.stock'));
        $show->view_amount(trans('admin.view_amount'));
        $show->sell_amount(trans('admin.sell_amount'));
        $show->sort(trans('admin.sort'));
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));
        $show->products('产品', function ($data) {
            $data->setResource('/products');
            $data->title(trans('admin.title'));
            $data->subtitle(trans('admin.subtitle'));
            $data->image(trans('admin.image'));
            $data->content(trans('admin.content'));
            $data->service_time(trans('admin.service_time'));
            $data->price(trans('admin.price'));
            $data->stock_amount(trans('admin.stock'));
            $data->view_amount(trans('admin.view_amount'));
            $data->sell_amount(trans('admin.sell_amount'));
            $data->filter(function ($filter) {
                $filter->column(1 / 3, function ($filter) {

                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->equal('title', trans('admin.title'));
                });
                $filter->column(1 / 3, function ($filter) {
                    $filter->between('price', trans('admin.price'));
                });
            });
        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $shop_id = Request('shop_id');

        $pro_options = Product::where(function ($query) use ($shop_id) {
//            $query->where('shop_id', $shop_id);
        })->pluck('title', 'id');

        $form = new Form(new Package);
        $form->select('shop_id', trans('admin.shop'))->options(Shop::where('status', Shop::PASS)->pluck('name', 'id'))->default($shop_id)->rules('required')->setLabelClass(['required']);
        $form->text('title', trans('admin.title'))->rules('required')->setLabelClass(['required']);
        $form->text('subtitle', trans('admin.subtitle'));
        $form->image('image', trans('admin.image'));
        $form->editor('content', trans('admin.content'));
        $form->text('service_time', trans('admin.service_time'));
        $form->decimal('price', trans('admin.price'))->default(0.00);
        $form->listbox('products', trans('admin.product'))->options($pro_options);
        $form->number('stock_amount', trans('admin.stock'))->default(0)->min(0);
        $form->number('view_amount', trans('admin.view_amount'))->default(0)->min(0);
        $form->number('sell_amount', trans('admin.sell_amount'))->default(0)->min(0);
        $form->number('sort', trans('admin.sort'))->default(0);
        $form->radio('is_show', trans('admin.show'))->options(Package::$IS_SHOW_MAP);
//        $form->disableEditingCheck();
        $form->disableCreatingCheck();
//        $form->disableViewCheck();
        $form->tools(function (Form\Tools $tools) use ($shop_id) {
            $tools->disableDelete();
            $tools->disableView();

            if ($shop_id) {
                $tools->disableList();
                $tools->append('<div class="btn-group pull-right" style="margin-right: 5px"><a href="' . route('shops.show', ['shop' => $shop_id]) . '" class="btn btn-sm btn-primary" title="' . trans('admin.back') . '"><i class="fa fa-backward"></i><span class="hidden-xs">&nbsp;' . trans('admin.back') . '</span></a></div>');
            } else {
                $tools->append('<div class="btn-group pull-right" style="margin-right: 5px"><a href="javascipt:;" onClick="javascript :history.back();" class="btn btn-sm btn-primary" title="' . trans('admin.back') . '"><i class="fa fa-backward"></i><span class="hidden-xs">&nbsp;' . trans('admin.back') . '</span></a></div>');
            }
        });
        $form->saved(function ($form) use ($shop_id) {
            if ($shop_id) {
                return redirect(route('shops.show', ['shop' => $shop_id]));
            }
        });

        return $form;
    }
}
